﻿@inherits BaseComponent
@attribute [NavItem]

<AntDropdown Context="Context" Model="userDrop">
    <div style="display:flex;align-items:center;margin-top:3px;">
        <Avatar Src="@AvatarUrl" />
        <span>@CurrentUser?.Name</span>
    </div>
</AntDropdown>

@code {
    private DropdownModel userDrop = new();
    private string AvatarUrl => CurrentUser?.AvatarUrl ?? "img/face1.png";
    [CascadingParameter] private TopNavbar Topbar { get; set; }

    protected override async Task OnInitAsync()
    {
        await base.OnInitAsync();
        userDrop.Items = new List<ActionInfo>();
        if (!Config.App.IsClient)
        {
            userDrop.Items.Add(new ActionInfo { Id = "profile", Name = Language["Nav.Profile"], Icon = "user", Url = "/profile" });
            if (CurrentUser?.IsSystemAdmin() == true && Config.IsDevelopment)
                userDrop.Items.Add(new ActionInfo { Id = "SysDevelopment", Name = Language["Nav.Development"], Icon = "appstore-add", Url = "/development" });
        }
        userDrop.Items.Add(new ActionInfo { Id = "logout", Name = Language["Nav.Exit"], Icon = "poweroff" });
        userDrop.OnItemClick = OnItemClickAsync;
    }

    private Task OnItemClickAsync(ActionInfo item)
    {
        if (!string.IsNullOrWhiteSpace(item.Url))
            Navigation?.NavigateTo(new MenuInfo { Id = item.Id, Name = item.Name, Icon = item.Icon, Url = item.Url });
        else
            Topbar?.OnMenuClick?.Invoke(item.Id);
        return Task.CompletedTask;
    }
}